#include <WProgram.h>

#include "CButtonHandler.hpp"
#include "Types.hpp"

CButtonHandler::CButtonHandler(uint8_t buttonPin)
: mButtonPin(buttonPin)
, mPressedTime(0)
, mPreviousState(LOW)
, mCurrentState(LOW)
{
}

CButtonHandler::init()
{
   pinMode(mButtonPin, INPUT);
}

uint8_t CButtonHandler::processButton()
{
   uint8_t retVal = BUTTON_INVALID;
   mCurrentState = digitalRead(mButtonPin);
   
   // determining if button value has changed
   if ((LOW == mPreviousState) && (HIGH == mCurrentState))
   {
      // Button is pressed
      retVal = BUTTON_PRESS;
   } else if ((HIGH == mPreviousState) && (LOW == mCurrentState))
   {
      // Button is released
      retVal = BUTTON_RELEASE;
   }
   
   mPreviousState = mCurrentState;
   
   return retVal;
}
